package com.haredot.security;

import com.haredot.properties.JwtProperties;
import com.haredot.utils.JwtUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功后执行的代码
 */
public class HaredotAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private JwtProperties jwtProperties ;

    private StringRedisTemplate stringRedisTemplate ;

    public HaredotAuthenticationSuccessHandler(JwtProperties jwtProperties, StringRedisTemplate stringRedisTemplate) {
        this.jwtProperties = jwtProperties ;
        this.stringRedisTemplate = stringRedisTemplate ;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        // 不生成刷新令牌
        JwtUtils.generatorToken(stringRedisTemplate, principal, jwtProperties, response, true, true, false);
    }

}
